package com.bai.order.controller;


import com.bai.order.service.PayLogService;
import com.bai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-08-19
 */
@RestController
@RequestMapping("/edu/payLog")
@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payloadLogService;

    //TODO:根据订单号生成微信支付二维码
    @GetMapping("creatNative/{orderNo}")
    public R creatNative(@PathVariable String orderNo) {
        Map map = payloadLogService.creatNative(orderNo);
        return R.ok().data("map", map);
    }

    //TODO:查询支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map<String, String> map = payloadLogService.queryPayStatus(orderNo);
        if (map == null) {
            return R.erro().message("支付出错");
        }
        if (map.get("trade_state").equals("success")) {
          
            //支付成功向订单表添加记录
            payloadLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中");
    }
}

